package com.ruoyi.web.controller.system;
 
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.AppUser;
import com.ruoyi.common.core.domain.model.LoginAppBody;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.domain.model.RegisterBody;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.web.service.AppLoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
 
/**
 * 登录验证
 *
 * @author ruoyi
 */
@Api("APP登录管理")
@RestController
@RequestMapping("/app")
public class AppLoginController extends BaseController {
    @Resource
    private AppLoginService appLoginService;
 
 
    @ApiOperation("登录")
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginAppBody loginAppBody) {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = appLoginService.login(loginAppBody.getUsername(), loginAppBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }
 
    @ApiOperation("根据token获取用户信息")
    @GetMapping("/userInfo")
    public AjaxResult userInfo() {
        AppUser appUser = getLoginAppUser().getAppUser();
        AjaxResult ajax = AjaxResult.success();
        ajax.put("appUser", appUser);
        return ajax;
    }

    @PostMapping("/register")
    public AjaxResult register(@RequestBody AppUser user)
    {
        appLoginService.register(user);
        return AjaxResult.success();
    }

    /**
     * 退出登录
     */
    @ApiOperation("退出登录")
    @PostMapping("/logout")
    public AjaxResult logout() {
        return AjaxResult.success();
    }

    /**
     * 获取个人信息
     */
    @ApiOperation("获取个人信息")
    @GetMapping("/user/profile")
    public AjaxResult profile() {
        AppUser user = getLoginAppUser().getAppUser();
        return AjaxResult.success(user);
    }

    /**
     * 修改个人信息
     */
    @ApiOperation("修改个人信息")
    @PutMapping("/user/profile")
    public AjaxResult updateProfile(@RequestBody AppUser user) {
        appLoginService.updateProfile(user);
        return AjaxResult.success();
    }

    /**
     * 修改密码
     */
    @ApiOperation("修改密码")
    @PutMapping("/user/profile/updatePwd")
    public AjaxResult updatePwd(String oldPassword, String newPassword) {
        if (StringUtils.isNotEmpty(newPassword) && StringUtils.isNotEmpty(oldPassword)) {
            return AjaxResult.warn("修改密码失败，旧密码不能为空");
        }
        if (appLoginService.checkPassword(oldPassword, getLoginAppUser().getAppUser().getPassword())) {
            return AjaxResult.warn("修改密码失败，旧密码错误");
        }
        appLoginService.updatePwd(newPassword);
        return AjaxResult.success();
    }

    /**
     * 头像上传
     */
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            if (appLoginService.updateUserAvatar(avatar))
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }
}